// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: SBZ3 Exit Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 77 : TYPE_SBZ3EXIT


// Static Values

// Tables

// Functions
reserve function AddPoints
reserve function RecordAnimation
	
function AddPoints
	//We used this function to quickly hand out Time and Ring Bonus at the end of 
	//SBZ3. Now, we return to the original behaviour in normal playthroughs, and
	//add a fully fledged act finish in Arcade Mode
	object.value5 = 0	
	switch stage.minutes
	case 0
		if stage.seconds < 30
			object.value5 = 50000
		else
			if stage.seconds < 45
				object.value5 = 10000
			else
				object.value5 = 5000
			end if
		end if
		break
	case 1
		if stage.seconds < 30
			object.value5 = 4000
		else
			object.value5 = 3000
		end if
		break
	case 2
		object.value5 = 2000
		break
	case 3
		object.value5 = 1000
		break
	case 4
		object.value5 = 500
		break
	case 5
		object.value5 = 100
		break
	case 9
		if stage.debugMode == 0
			if options.gameMode < 2
				if stage.seconds == 59
					object.value5 = 100000
				end if
			end if
		end if
		break
	end switch
	
	object.value6 = object[0].value0
	object.value6 *= 100
	
	object.value5 += object.value6
	if object.value5 > 0
		PlaySfx(SfxName[Score Total], 0)
		player.score += object.value5
	end if
end function

function RecordAnimation
	recAnimation = 0
	temp0 = 0
	currentPlayer = 0
	while currentPlayer < playerCount
		temp1 = object[currentPlayer].animation
		temp1 <<= 8
		temp1 |= object[currentPlayer].direction
		temp1 <<= temp0
		recAnimation |= temp1
		temp0 += 16
		currentPlayer++
	loop
end function

event ObjectMain
	switch object.state
	case 0
		BoxCollisionTest(C_TOUCH, object.entityPos, -128, -16, 128, 16, 0, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		if checkResult == 1
			if object[0].value37 == 5
				recGoggles = 5
			else
				recGoggles = 0
			end if
			object[0].type = TypeName[Ring]
			object[0].groupID = TypeName[Blank Object]
			object[0].controlMode = -1
			switch options.gameMode
				default //save or nosave
					object.state++
					stage.timeEnabled = 0
					//CallFunction(AddPoints)
					break
				case 2 //time attack
					recAnimation = 0
					if stage.timeEnabled == 1
						recGoggles = 0
						object.state = 2
						timeAttack.result = stage.seconds
						timeAttack.result *= 100
						temp0 = stage.minutes
						temp0 *= 0x1770
						timeAttack.result += temp0
						timeAttack.result += stage.milliSeconds
						//stage.listPos--
						switch stage.playerListPos
							case 0
								arrayPos0 = 500
								break
							case 1
								arrayPos0 = 530
								break
							case 2
								arrayPos0 = 560
								break
							case 5
								arrayPos0 = 470
								break		
							case 6
								arrayPos0 = 3700
								break						
							case 7
								arrayPos0 = 3750
								break						
							case 8
								arrayPos0 = 3790
								break						
							case 9
								arrayPos0 = 3830
								break						
						end switch
						if SuperTimeAttack == 1
							arrayPos0 = 590
						end if
						arraypos0 += stage.listPos
						if timeAttack.result < saveRAM[arrayPos0]
							saveRAM[arrayPos0] = timeAttack.result
							//object[9].value2 = timeAttack.result
							PlaySfx(SfxName[Event], 0)
						end if
						WriteSaveRAM()
					end if
					stage.timeEnabled = 0
					break
				case 3 //arcade					
					object.state = 2
					stage.timeEnabled = 0
					break
			end switch
		end if
		break
	case 1
		if object.value0 < 512
			object.value0 += 8
			music.volume -= 2
		else
			object.direction = FLIP_NONE
			lampPostID = 0
			stage.listPos++
			if options.gameMode == 1
				arrayPos1 = options.saveSlot
				arrayPos1 <<= 3
					arrayPos1 += 1000
				if stage.player2Enabled == 1
					switch stage.playerListPos
						case 0
							saveRAM[arrayPos1] = 3
							break
						case 2
							saveRAM[arrayPos1] = 4
							break
					end switch
				else
					saveRAM[arrayPos1] = stage.playerListPos
				end if
				arrayPos1++
				saveRAM[arrayPos1] = player.lives
				arrayPos1++
				saveRAM[arrayPos1] = player.score
				arrayPos1++
				saveRAM[arrayPos1] = player.scoreBonus
				arrayPos1++
				if saveRAM[arrayPos1] < 20
					saveRAM[arrayPos1] = stage.listPos
					saveRAM[arrayPos1]++
				end if
				arrayPos1++
				saveRAM[arrayPos1] = specialStage.emeralds
				arrayPos1++
				saveRAM[arrayPos1] = specialStage.listPos
			end if
			CallFunction(RecordAnimation)
			WriteSaveRAM()
			LoadStage()
		end if
		SetClassicFade(0, 0, 0, object.value0)
		break
	case 2
		if object.value0 < 60
			object.value0++
			music.volume -= 2
		else
			CallFunction(RecordAnimation)
			object.type = TypeName[Blank Object]
			object[30].type = TypeName[Act Finish]
			object[30].drawOrder = 6
			object[30].priority = PRIORITY_ACTIVE
			PlayMusic(1)
		end if
		break
	end switch
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
